package com.cmsz.jiangxm.MyBatisTest.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.cmsz.jiangxm.MyBatisTest.model.PageInfo;
import com.cmsz.jiangxm.MyBatisTest.model.Result;
import com.cmsz.jiangxm.MyBatisTest.model.User;
import com.cmsz.jiangxm.MyBatisTest.service.UserService;

@RestController
@RequestMapping("/user")
public class UserController {

	private static final Logger logger = LoggerFactory.getLogger(UserController.class);

	@Autowired
	private UserService userService;

	@RequestMapping("/hello")
	@ResponseBody
	public String hello() {
		return "hello";
	}

	/**
	 * 测试分页插件
	 * 
	 * @return
	 */
	@RequestMapping(value="/queryByPage",consumes = "application/json")
	@ResponseBody
	public Result queryPage() {
		// List<User> name_list = new ArrayList<>();
		PageInfo<User> page = this.userService.queryByPage("jiangxm", 2, 2);
		System.out.println("总页数=" + page.getPages());
		System.out.println("总记录数=" + page.getTotal());
		for (User u : page.getList()) {
			System.out.println(u.getId() + " \t " + u.getName());
			// name_list.add(u);
		}
//		return page.getList();
		// return "success";
//		return true;
		List<String> numbers = new ArrayList<String>();
		numbers.add("17050000000");
		numbers.add("17050000001");
		numbers.add("17050000002");
		numbers.add("17050000003");
		Result result = new Result(true,"查询成功",numbers);
		return result;
	}

	/**
	 * 测试事务
	 * 
	 * @return
	 */
	@RequestMapping("/testTransational")
	@ResponseBody
	public String test() {
		try {
			this.userService.testTransational();
			return "success";
		} catch (Exception e) {
			return e.getMessage();
		}

	}

	@RequestMapping(value = "/testTransational1", consumes = "application/json")
	@ResponseBody
	public List<String> test1() {

		List<String> list = new ArrayList<>(Arrays.asList("1233445", "132467", "456488"));
		return list;
	}
}

class User1 {
	public User1() {
	}

	List<String> list = new ArrayList<>(Arrays.asList("1233445", "132467", "456488"));

	public List<String> getList() {
		return list;
	}

	public void setList(List<String> list) {
		this.list = list;
	}

}
